# -*- coding: utf-8 -*-
# @Time : 2024/1/20 20:38
# @Author : cute
# @Email : Aggressive_cute@126.com
import functools
from sanic.response import text
from sanic.request import Request
from sanic import Blueprint, json
from .yp.core import CloudSheet
from settings.server_settings import white_list

yp_api = Blueprint('yp_api', url_prefix='/cute_api/yp_api')


# 验签
def verify_signature(func):
    invalid_info = 'Invalid Signature 请联系管理员(v):  Python_Cute2023'
    @functools.wraps(func)
    def wrapper(*args, **kw):
        request = args[0]
        signature = request.headers.get('signature')
        if not signature:
            return text(invalid_info, status=403)

        if signature not in white_list:  # 白名单
            return text(invalid_info, status=403)

        return func(*args, **kw)

    return wrapper


# /cute_api/yp_api/slide
@yp_api.route("/slide", methods=["POST", "GET"])
@verify_signature
async def yp_slide(request):
    """POST JSON{fp/ detail}"""
    data_dict = None
    if request.method != "POST":
        data_dict = {
            "code": -1,
            "msg": "failed",
            "data": "请使用POST请求传参！"
        }
        return json(data_dict)
    try:
        client_dict = request.json
        captcha_id = client_dict.get('captcha_id')
        yp_riddler_id = client_dict.get('yp_riddler_id')
        fp = client_dict.get('fp')

        if captcha_id and yp_riddler_id and fp:
            params_dict = {
                'fp': fp,
                'captcha_id': captcha_id,
                'yp_riddler_id': yp_riddler_id,
            }
            data_dict = {
                "code": 0,
                "msg": "success",
                "data": CloudSheet(**params_dict).pass_slide()
            }
        else:
            data_dict = {
                "code": -1,
                "msg": "failed",
                "data": "请传入完整参数"
            }
            return json(data_dict)


    except Exception as e:
        data_dict = {
            "code": -1,
            "msg": "failed",
            "data": str(e)
        }

    return json(data_dict)


